package com.dl.study.module.base.authority.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dl.study.base.PageResultDTO;
import com.dl.study.util.resp.ResponseDTO;
import com.dl.study.module.base.authority.dao.AuthorityDao;
import com.dl.study.module.base.authority.domain.dto.AuthorityDTO;
import com.dl.study.module.base.authority.domain.dto.AuthorityQueryDTO;
import com.dl.study.module.base.authority.domain.dto.AuthorityListQueryDTO;
import com.dl.study.module.base.authority.domain.entity.AuthorityEntity;
import com.dl.study.util.base.PaginationUtil;
import com.dl.study.util.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 权限表 ]
 *
 * @author dl
 * @version 1.0
 * @company xxxx
 * @copyright (c) 2019 xxxxInc. All rights reserved.
 * @date 2021-10-28 16:13:14
 * @since JDK1.8
 */
@Service
public class AuthorityService {

    @Autowired
    private AuthorityDao authorityDao;


    /**
     * @author dl
     * @description 分页查询
     * @date 2021-10-28 16:13:14
     */
    public ResponseDTO<PageResultDTO<AuthorityDTO>> queryByPage(AuthorityQueryDTO queryDTO) {
        Page page = PaginationUtil.convert2PageQueryInfo(queryDTO);
        AuthorityListQueryDTO listQueryDTO = BeanCopyUtil.copy(queryDTO, AuthorityListQueryDTO.class);
        List<AuthorityDTO> dtoList = authorityDao.queryListByCondition(page, listQueryDTO);
        page.setRecords(dtoList);
        PageResultDTO<AuthorityDTO> pageResultDTO = PaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * @author dl
     * @description 根据条件查询列表
     * @date 2021-10-28 16:13:14
     */
    public ResponseDTO<List<AuthorityDTO>> queryListByCondition(AuthorityListQueryDTO listQueryDTO) {
        List<AuthorityDTO> dtoList = authorityDao.queryListByCondition(listQueryDTO);
        return ResponseDTO.succData(dtoList);
    }

    /**
     * @author dl
     * @description 添加
     * @date 2021-10-28 16:13:14
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(AuthorityDTO addDTO) {
        AuthorityEntity entity = BeanCopyUtil.copy(addDTO, AuthorityEntity.class);
        authorityDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author dl
     * @description 编辑
     * @date 2021-10-28 16:13:14
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(AuthorityDTO updateDTO) {
        AuthorityEntity entity = BeanCopyUtil.copy(updateDTO, AuthorityEntity.class);
        authorityDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author dl
     * @description 删除
     * @date 2021-10-28 16:13:14
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        authorityDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author dl
     * @description 根据ID查询
     * @date 2021-10-28 16:13:14
     */
    public ResponseDTO<AuthorityDTO> detail(Long id) {
        AuthorityEntity entity = authorityDao.selectById(id);
        AuthorityDTO dto = BeanCopyUtil.copy(entity, AuthorityDTO.class);
        return ResponseDTO.succData(dto);
    }

    /**
     * @author dl
     * @description 批量删除
     * @date 2021-10-28 16:13:14
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> ids) {
        authorityDao.deleteByIds(ids);
        return ResponseDTO.succ();
    }


    /**
     * @author dl
     * @description 批量新增
     * @date 2021-10-28 16:13:14
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> addList(List<AuthorityDTO> addList) {
        List<AuthorityEntity> entityList = BeanCopyUtil.copyList(addList, AuthorityEntity.class);
        authorityDao.insertBatch(entityList);
        return ResponseDTO.succ();
    }
}
